$(function () {


    //点击列表中"详情"按钮
    // $(".pro_area_detail").click(function(){})


        $("#pro_area_modals").on('show.bs.modal', function (event){
            var _e = $(event.relatedTarget);
            var adcode = _e.data("code");
            $("#adcode").val(adcode);
            resetData(1);
        });



    function resetData(page){
        var area =  $("#area").val();
        var prodct = $('#prodct').val();
        var adcode =$("#adcode").val();
        var baseId = $("#hidden_id").val();
        var state = $("#state").val();

        page = page || 1;
        var  url ="/salemanPayrule/getProdAndAreaList?adcode="+adcode+"&baseId="+baseId + "&prodct="+prodct + "&area="+area + "&state="+state +"&page="+page;

        $.post(url, function (re) {
            var result = re.data, resultList=result.result;
            if (result) {
                var str="";
                if(resultList.length >0){
                    for(var i=0; i<resultList.length; i++){
                        var sta=resultList[i].state;
                        var state="";
                        if(sta==0){
                            state="未启用";
                        }else if(sta ==1){
                            state="正常";
                        }else if(sta ==2){
                            state="封存";
                        }
                        str += '<tr>' +
                            '<td>'+ resultList[i].adname + '</td>'+
                            '<td>'+  resultList[i].proName + '</td>'+
                            '<td>'+state + '</td>';
                    }
                }else{
                    str += '<tr>'+
                                '<td class="text-center text-muted" colspan="3"><i class="fa fa-info-circle"></i> 没有记录</td>'+
                        '</tr>';
                }

                $('#product_model').html(str);
                resetPage(result.pageTotal, page);
                // $("#proTotal").text(result.resultTotal);
            }
        });
    }

    function resetPage(totalPage, currPage){
        var innerHTML = [];
        currPage = currPage || 1;
        innerHTML.push('<ul class="pagination pagination-sm no-margin">');
        if(currPage > 1){
            innerHTML.push('<li class="prev"><a href="javascript:;"><span>上一页</span></a></li><li><a href="javascript:;"><span>1</span></a></li>');
        }
        if(currPage - 4 > 1){
            innerHTML.push('<li class="bigPrev"><a href="javascript:;"><span>...</span></a></li>')
        }
        for(var i = currPage - 3; i < currPage; i++){
            if(i > 1){
                innerHTML.push('<li><a href="javascript:;"><span>',i,'</span></a></li>');
            }
        }
        innerHTML.push('<li class="active"><span>', currPage,'</span></li>')
        for(var i = currPage+1; i < currPage+3; i++){
            if(i < totalPage){
                innerHTML.push('<li><a href="javascript:;"><span>',i,'</span></a></li>');
            }
        }
        if(currPage < totalPage - 3){
            innerHTML.push('<li class="bigNext"><a href="javascript:;"><span>...</span></a></li>');
        }
        if(currPage < totalPage){
            innerHTML.push('<li><a href="javascript:;"><span>', totalPage,'</span></a></li>',
                '<li class="next"><a href="javascript:;"><span>下一页</span></a></li>');
        }
        innerHTML.push("</ul>")
        $("#page_model").data("total", totalPage).html(innerHTML.join(""));
    }

    $("#pro_query").click(function () {
        resetData(1);
    });

    $("#page_model").on("click", "li", function () {
        if($(this).hasClass("active")){
            return false;
        }
        var currPage = parseInt($("#page_model .active").text()),
            totalPage = $("#page_model").data("total");
        if($(this).hasClass("prev")){
            currPage--;
        } else if($(this).hasClass("next")){
            currPage++;
        } else if($(this).hasClass("bigPrev")){
            currPage -= 5;

            if(currPage < 1){
                currPage = 1;
            }
        } else if($(this).hasClass("bigNext")){
            currPage += 5;

            if(totalPage - currPage < 0){
                currPage = totalPage;
            }
        } else {
            currPage = parseInt($(this).text());
        }

        resetData(currPage);
    });



});